package com.jams.goodox.activity.serve.enterprise;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;

import com.hangzhou.jin.customview.LoadView;
import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.entity.resp.EnterpriseContactsResp;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.EnterpriseService;
import com.jams.goodox.utils.CollectionUtils;
import com.jams.goodox.utils.DataCacheManager;
import com.jams.goodox.utils.StringUtils;
import com.jams.goodox.utils.StyleUtil;
import com.jams.library.commonAdapter.ListCommonAdapter;
import com.jams.library.commonAdapter.ListCommonViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import retrofit2.Call;


/**
 * 企业对外联系人
 */
public class EnterpriseExternalContactsActivity extends BaseActivity {
    @Bind(R.id.empty_view)
    LoadView emptyView;
    @Bind(R.id.lv_enterprise_external_contacts)
    ListView lvEnterpriseDirectory;
    ListCommonAdapter<EnterpriseContactsResp.EnterpriseContactsData.EnterpriseContactsResultListBean> mAdapter;
    List<EnterpriseContactsResp.EnterpriseContactsData.EnterpriseContactsResultListBean> staffBeanList = new ArrayList<>();
    public static final String ENTERPRISE_ID = "ENTERPRISE_ID";
    public static final String ENTERPRISE_NAME = "ENTERPRISE_NAME";
    /**
     * 传入的企业id
     */
    private String enterpriseId;

    /**
     * 传入的企业名称
     */
    private String enterpriseName;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_enterprise_external_contacts;
    }

    @Override
    protected void init() {
        Intent i = getIntent();
        enterpriseId = StringUtils.newString(i.getStringExtra(ENTERPRISE_ID));
        enterpriseName = StringUtils.newString(i.getStringExtra(ENTERPRISE_NAME));
        StyleUtil.titleBackKey(this, enterpriseName);
        mAdapter = new ListCommonAdapter<EnterpriseContactsResp.EnterpriseContactsData.EnterpriseContactsResultListBean>(mContext, staffBeanList, R.layout.item_enterprise_external_contacts) {
            @Override
            public void convert(ListCommonViewHolder helper, final EnterpriseContactsResp.EnterpriseContactsData.EnterpriseContactsResultListBean item, int position) {
               /* switch (item.externalIdentity) {
                    case "1":
                        helper.setText(R.id.tv_tittle, "CEO").setViewVisible(R.id.tv_tittle, true);
                        break;
                    case "2":
                        helper.setText(R.id.tv_tittle, "COO").setViewVisible(R.id.tv_tittle, true);
                        break;
                    case "3":
                        helper.setText(R.id.tv_tittle, "CMO").setViewVisible(R.id.tv_tittle, true);
                        break;
                    default:
                        helper.setViewVisible(R.id.tv_tittle, false);
                        break;*/
                helper.setText(R.id.tv_tittle, item.externalIdentity).setViewVisible(R.id.tv_tittle, !TextUtils.isEmpty(item.externalIdentity));
                

                if (1 == item.sex) {
                    helper.setImageBackground(R.id.sex, R.mipmap.male_icon);
                } else if (2 == item.sex) {
                    helper.setImageBackground(R.id.sex, R.mipmap.female_icon);
                }

                if(StringUtils.newString(item.userId).equals(StringUtils.newString(DataCacheManager.getInstance().getUser().getUserId()))){
                    helper.setViewVisible(R.id.iv_message_mail,false);
                }else{
                    helper.setViewVisible(R.id.iv_message_mail,true);
                }
                helper.setSimpleDraweeView(R.id.head, StringUtils.newString(item.headPicName))
                        .setText(R.id.tv_customer_name, item.userName)
                        .getView(R.id.iv_message_mail).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                    }
                });

            }
        };
        lvEnterpriseDirectory.setAdapter(mAdapter);
        getData();
    }

    /**
     * 获取数据
     */
    private void getData() {
        showLoading();
        enqueue(createCallService(EnterpriseService.class).getEnterpriseExternalList(enterpriseId), new RetrofitCallbackListener<EnterpriseContactsResp>() {
            @Override
            public void onSuccess(Call<EnterpriseContactsResp> call, EnterpriseContactsResp result) {
                misLoading();
                try {
                    if (!CollectionUtils.hasData(result.data.resultList)) {
                        emptyView.loadCompleteNoData(R.mipmap.holder_no_common,R.string.no_content);
                    }else {
                        staffBeanList.clear();
                        staffBeanList.addAll(result.data.resultList);
                        mAdapter.notifyDataSetChanged();
                        emptyView.loadComplete();
                    }
                    
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(String msg) {
                misLoading();
                try {
                    if (!CollectionUtils.hasData(staffBeanList)) {
                        emptyView.loadCompleteNoData(R.mipmap.holder_no_common,R.string.no_content);
                    } else {
                        emptyView.loadComplete();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
